###重启不同的weblogic 服务
#!/bin/sh
dirArray=("userCenter" "merchants" "payment")
serverArray=("UserServer" "MerServer" "paymentServer")
now=`date "+%Y%m%d%H"`
echo current date is $now
weblogicServerBaseDir="/home/weblogic/bea/user_projects/domains/base_domain/servers/"
weblogicBinBaseDir="/home/weblogic/bea/user_projects/domains/base_domain/bin/"
warBaseDir="/apps/besp/"

backupDir="/apps/besp/backup/"

#if backupdir not exist create it

if [ ! -d "$backupDir" ] ; then
  echo "$backupDir"not exist, create it...
  mkdir "$backupDir"
else 
   echo "$backupDir"exist, continue...
fi

for i in "${!dirArray[@]}"; do
    #printf "%s\t%s\n" "$i" "${dirArray[$i]}"
    #start for loop
    cd $warBaseDir
    dir="${dirArray[$i]}"
    server="${serverArray[$i]}"
   
    #tar directory
    tar -zcf "$backupDir/$dir$now".tar.gz $dir
    #delete directory
    echo delete directory $dir...
    rm -rf $dir
    #unzip new package

    #unzip -o $dir.war -d $dir -p
    unzip -q -o -d $dir $dir.war
    echo kill $server process...
    kill -9 `ps -ef|grep $server |grep -v grep|awk '{print $2}'`
    #change work diretory to weblogic servers
    echo change dir to "$weblogicServerBaseDir$server"
    cd "$weblogicServerBaseDir$server"
    echo remove "$server" cache
    rm -rf logs
    rm -rf tmp
    rm -rf cache
    rm -rf stage
    echo change dir to "$weblogicBinBaseDir"
    cd $weblogicBinBaseDir
    rm -rf $dir.out
    sleep 2s
    if [ "x"`ps -ef|grep $server |grep -v grep|awk '{print $2}'` == "x" ]; then
        echo "$server has been stoped successfully."
            echo start weblogic "$server"
            # AdminServer use startWebLogic.sh
            #otherServer usr startManagedWebLogic.sh
            if [  "x$server" == "xAdminServer" ];then
              nohup ./startWebLogic.sh > $dir.out 2>&1 &
            else
              nohup ./startManagedWebLogic.sh $server> $dir.out 2>&1 &
            fi

            while true
            do
              result=`cat $weblogicBinBaseDir$dir.out |grep "RUNNING mode"`
              result="${result:0-14:7}"
              if [[ "$result"x == "RUNNING"x ]]; then
                  echo -e "\n\033[32;40;7mstart weblogic $server success..\033[0m\n"
                  break
              else
                 sleep 1s
                 result=`cat $weblogicBinBaseDir$dir.out |grep FORCE_SHUTTING_DOWN`
                 result="${result:0-20:19}"
                 if [[ "$result"x == "FORCE_SHUTTING_DOWN"x ]]; then
                    echo -e "\033[31;43;7mweblogic $server start failed. please check $dir.out.\033[0m"
                    break;
                 fi
                 echo -e ".\c"
                 continue
              fi
            done
            echo ""
            #tail -f $dir.out
    else
       echo "$server stop failed retry"
       kill -9 `ps -ef|grep $server |grep -v grep|awk '{print $2}'`
       sleep 2s
    fi
    #end for loop
done


echo -e "\n\033[32;40;7mAll server start end.Please check the weblogic console.\033[0m"

